package com.alibaba.dingtalk.openapi.helper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dingtalkim_1_0.models.BatchQueryGroupMemberHeaders;
import com.aliyun.dingtalkim_1_0.models.BatchQueryGroupMemberRequest;
import com.aliyun.dingtalkim_1_0.models.BatchQueryGroupMemberResponse;
import com.aliyun.dingtalkim_1_0.models.BatchQueryGroupMemberResponseBody;
import com.aliyun.dingtalkim_1_0.models.GetSceneGroupInfoHeaders;
import com.aliyun.dingtalkim_1_0.models.GetSceneGroupInfoRequest;
import com.aliyun.dingtalkim_1_0.models.GetSceneGroupInfoResponse;
import com.aliyun.dingtalkim_1_0.models.GetSceneGroupInfoResponseBody;
import com.aliyun.dingtalkim_1_0.models.InteractiveCardCreateInstanceHeaders;
import com.aliyun.dingtalkim_1_0.models.InteractiveCardCreateInstanceRequest;
import com.aliyun.dingtalkim_1_0.models.InteractiveCardCreateInstanceResponse;
import com.aliyun.dingtalkim_1_0.models.PrivateDataValue;
import com.aliyun.dingtalkim_1_0.models.SendInteractiveCardHeaders;
import com.aliyun.dingtalkim_1_0.models.SendInteractiveCardRequest;
import com.aliyun.dingtalkim_1_0.models.SendInteractiveCardResponse;
import com.aliyun.dingtalkim_1_0.models.SendRobotInteractiveCardHeaders;
import com.aliyun.dingtalkim_1_0.models.SendRobotInteractiveCardRequest;
import com.aliyun.dingtalkim_1_0.models.SendRobotInteractiveCardResponse;
import com.aliyun.dingtalkim_1_0.models.TopboxCloseHeaders;
import com.aliyun.dingtalkim_1_0.models.TopboxCloseRequest;
import com.aliyun.dingtalkim_1_0.models.TopboxCloseResponse;
import com.aliyun.dingtalkim_1_0.models.TopboxOpenHeaders;
import com.aliyun.dingtalkim_1_0.models.TopboxOpenRequest;
import com.aliyun.dingtalkim_1_0.models.TopboxOpenResponse;
import com.aliyun.dingtalkim_1_0.models.UpdateInteractiveCardHeaders;
import com.aliyun.dingtalkim_1_0.models.UpdateInteractiveCardRequest;
import com.aliyun.dingtalkim_1_0.models.UpdateInteractiveCardResponse;
import com.aliyun.dingtalkim_1_0.models.UpdateRobotInteractiveCardHeaders;
import com.aliyun.dingtalkim_1_0.models.UpdateRobotInteractiveCardRequest;
import com.aliyun.dingtalkim_1_0.models.UpdateRobotInteractiveCardResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.constant.BasicConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * @Author: hongxiao
 * @date: 2022-06-07 10:54
 */
@Component
public class DingCoolAppHelper {
    private static final Logger LOG = LoggerFactory.getLogger(DingCoolAppHelper.class);


    /***
     * 获取酷应用群信息
     *
     * @param: accessToken 访问钉钉接口accessToken
     * @param: openConversationId 群id
     * @param: coolAppCode 酷应用code
     * @throws
     * @return GetSceneGroupInfoResponseBody
     * @author hongxiao
     * @date 2022-06-07 12:17
     * @since
     * @version
     */
    public GetSceneGroupInfoResponseBody getGroupInfo(String accessToken, String openConversationId, String coolAppCode) {
        com.aliyun.dingtalkim_1_0.Client client;
        try {
            client = ClientSample.createClient();
        } catch (Exception e) {
            LOG.error("生成client报错");
            return null;
        }
        if (Objects.isNull(accessToken)) {
            LOG.error("accessToken为空");
            return null;
        }
        GetSceneGroupInfoHeaders getSceneGroupInfoHeaders = new GetSceneGroupInfoHeaders();
        getSceneGroupInfoHeaders.xAcsDingtalkAccessToken = accessToken;
        GetSceneGroupInfoRequest getSceneGroupInfoRequest = new GetSceneGroupInfoRequest();
        getSceneGroupInfoRequest.setOpenConversationId(openConversationId);
        getSceneGroupInfoRequest.setCoolAppCode(coolAppCode);

        GetSceneGroupInfoResponse sceneGroupInfoResponse;

        try {
            sceneGroupInfoResponse = client.getSceneGroupInfoWithOptions(getSceneGroupInfoRequest, getSceneGroupInfoHeaders, new RuntimeOptions());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("获取群信息报错TeaException", err);
            sceneGroupInfoResponse = null;
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("获取群信息报错Exception", err);
            sceneGroupInfoResponse = null;
        }
        if (Objects.isNull(sceneGroupInfoResponse) || !sceneGroupInfoResponse.getBody().getSuccess()) {
            return null;
        }

        return sceneGroupInfoResponse.getBody();

    }

    /**
     *　获取群成员
     *
     * @param accessToken 访问钉钉接口accessToken
     * @param openConversationId 群id
     * @param coolAppCode 酷应用code
     * @return
     */
    public List<String> getGroupMembers(String accessToken, String openConversationId, String coolAppCode) {

        com.aliyun.dingtalkim_1_0.Client client;
        try {
            client = ClientSample.createClient();
        } catch (Exception e) {
            LOG.error("生成client报错");
            return null;
        }
        if (Objects.isNull(accessToken)) {
            LOG.error("accessToken为空");
            return null;
        }
        BatchQueryGroupMemberHeaders batchQueryGroupMemberHeaders = new BatchQueryGroupMemberHeaders();
        batchQueryGroupMemberHeaders.xAcsDingtalkAccessToken = accessToken;
        BatchQueryGroupMemberRequest batchQueryGroupMemberRequest = new BatchQueryGroupMemberRequest();
        batchQueryGroupMemberRequest.setOpenConversationId(openConversationId);
        batchQueryGroupMemberRequest.setCoolAppCode(coolAppCode);
        // 每次100人
        batchQueryGroupMemberRequest.setMaxResults(100L);
        boolean hasMore;
        // 防止钉钉异常
        int requestNum = 0;
        BatchQueryGroupMemberResponseBody body;

        List<String> userIdList = new ArrayList<>();

        do {
            body = getGroupMembers(client, batchQueryGroupMemberHeaders, batchQueryGroupMemberRequest);
            requestNum ++;
            hasMore = Objects.nonNull(body) && body.getSuccess() ? body.getHasMore() : false;
            if (Objects.nonNull(body) && body.getSuccess()) {
                userIdList.addAll(body.getMemberUserIds());

                batchQueryGroupMemberHeaders.xAcsDingtalkAccessToken = body.getNextToken();
            }
        } while (hasMore && requestNum < 100);

        return userIdList;
    }

    /***
     * 请求群成员
     *
     * @param: client
     * @param: batchQueryGroupMemberHeaders 请求头
     * @param: batchQueryGroupMemberRequest　请求体
     * @throws
     * @return BatchQueryGroupMemberResponseBody 返回实体
     * @author hongxiao
     * @date 2022-06-10 17:02
     * @since
     * @version
     */
    private BatchQueryGroupMemberResponseBody getGroupMembers(com.aliyun.dingtalkim_1_0.Client client, BatchQueryGroupMemberHeaders batchQueryGroupMemberHeaders, BatchQueryGroupMemberRequest batchQueryGroupMemberRequest) {
        BatchQueryGroupMemberResponse batchQueryGroupMemberResponse;
        try {
            batchQueryGroupMemberResponse = client.batchQueryGroupMemberWithOptions(batchQueryGroupMemberRequest, batchQueryGroupMemberHeaders, new RuntimeOptions());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("获取群成员报错TeaException", err);
            batchQueryGroupMemberResponse = null;

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("获取群成员报错Exception", err);
            batchQueryGroupMemberResponse = null;
        }

        if (Objects.isNull(batchQueryGroupMemberResponse) || !batchQueryGroupMemberResponse.getBody().getSuccess()) {
            return null;
        }

        return batchQueryGroupMemberResponse.getBody();
    }

    /**
     * 机器人发送互动卡片
     *
     * @param openConversationId 群id
     * @param cardTemplateId 卡片id
     * @param cardData　卡片公共数据
     * @param cardPrivateData 卡片私有数据
     * @return
     * @throws XbbException
     * @author hongxiao
     * @date 2022-06-09 21:12
     * @since
     * @version
     */
    public String sendRobotInteractiveCard(String accessToken, String openConversationId, String robotCode, String cardTemplateId, String callbackUrl, String cardBizId,
                                           JSONObject cardData, JSONObject cardPrivateData,
                                           SendRobotInteractiveCardRequest.SendRobotInteractiveCardRequestSendOptions sendOptions) {
        com.aliyun.dingtalkim_1_0.Client client;
        try {
            client = ClientSample.createClient();
        } catch (Exception e) {
            LOG.error("生成client报错");
            return null;
        }
        if (Objects.isNull(accessToken)) {
            LOG.error("accessToken为空");
            return null;
        }
        SendRobotInteractiveCardHeaders sendRobotInteractiveCardHeaders = new SendRobotInteractiveCardHeaders();
        sendRobotInteractiveCardHeaders.xAcsDingtalkAccessToken = accessToken;

        SendRobotInteractiveCardRequest sendRobotInteractiveCardRequest = new SendRobotInteractiveCardRequest();
        // 回调地址
        if (StringUtil.isNotEmpty(callbackUrl)) {
            sendRobotInteractiveCardRequest.setCallbackUrl(callbackUrl);
        }
        // 卡片cardBizId
        sendRobotInteractiveCardRequest.setCardBizId(cardBizId);
        if (Objects.nonNull(cardData)) {
            // 卡片数据
            sendRobotInteractiveCardRequest.setCardData(cardData.toJSONString());
        }
        // 卡片模板id
        sendRobotInteractiveCardRequest.setCardTemplateId(cardTemplateId);
        // 群id
        sendRobotInteractiveCardRequest.setOpenConversationId(openConversationId);
        // 机器人code
        sendRobotInteractiveCardRequest.setRobotCode(robotCode);
        // 发送选项
        sendRobotInteractiveCardRequest.setSendOptions(sendOptions);
        // 私有数据
        if (Objects.nonNull(cardPrivateData)) {
            sendRobotInteractiveCardRequest.setUserIdPrivateDataMap(cardPrivateData.toJSONString());
        }

        SendRobotInteractiveCardResponse cardResponse;
        try {
            cardResponse = client.sendRobotInteractiveCardWithOptions(sendRobotInteractiveCardRequest, sendRobotInteractiveCardHeaders, new RuntimeOptions());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("发送机器人卡片失败TeaException", err);
            cardResponse = null;

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("发送机器人卡片失败Exception", err);
            cardResponse = null;
        }
        if (Objects.isNull(cardResponse)) {
            return null;
        }

        return cardResponse.getBody().getProcessQueryKey();
    }

    /**
     * 更新机器人发送互动卡片
     *
     * @param cardBizId
     * @param cardData
     * @param cardPrivateData
     * @return
     * @throws XbbException
     * @author hongxiao
     * @date 2022-06-09 21:12
     * @since
     * @version
     */
    public String updateRobotInteractiveCard(String accessToken, String cardBizId, JSONObject cardData, JSONObject cardPrivateData) {
        com.aliyun.dingtalkim_1_0.Client client;
        try {
            client = ClientSample.createClient();
        } catch (Exception e) {
            LOG.error("生成client报错");
            return null;
        }
        if (Objects.isNull(accessToken)) {
            LOG.error("accessToken为空");
            return null;
        }
        UpdateRobotInteractiveCardHeaders updateRobotInteractiveCardHeaders = new UpdateRobotInteractiveCardHeaders();
        updateRobotInteractiveCardHeaders.xAcsDingtalkAccessToken = accessToken;

        UpdateRobotInteractiveCardRequest updateRobotInteractiveCardRequest = new UpdateRobotInteractiveCardRequest();
        updateRobotInteractiveCardRequest.setCardBizId(cardBizId);
        updateRobotInteractiveCardRequest.setCardData(cardData.toJSONString());
        updateRobotInteractiveCardRequest.setCardData(cardData.toJSONString());
        updateRobotInteractiveCardRequest.setUserIdPrivateDataMap(cardPrivateData.toJSONString());
        // 发送选项，默认全局更新
        updateRobotInteractiveCardRequest.setUpdateOptions(null);

        UpdateRobotInteractiveCardResponse cardResponse;
        try {
            cardResponse = client.updateRobotInteractiveCardWithOptions(updateRobotInteractiveCardRequest, updateRobotInteractiveCardHeaders, new RuntimeOptions());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("更新机器人卡片失败Exception", err);
            cardResponse = null;

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("更新机器人卡片失败Exception", err);
            cardResponse = null;
        }

        if (Objects.isNull(cardResponse)) {
            return null;
        }

        return cardResponse.getBody().getProcessQueryKey();
    }

    /***
     * 发送互动卡片
     *
     * @param: accessToken
     * @param: openConversationId
     * @param: robotCode
     * @param: cardTemplateId
     * @param: callbackRouteKey
     * @param: cardBizId
     * @param: cardData
     * @param: cardPrivateData
     * @param: sendOptions
     * @param: receiverUserIdList
     * @param: atOpenIds
     * @throws
     * @return java.lang.String
     * @author hongxiao
     * @date 2022-06-13 14:53
     * @since
     * @version
     */
    public String sendInteractiveCard(String accessToken, String openConversationId, String robotCode, String cardTemplateId, String callbackRouteKey, String cardBizId,
                                      SendInteractiveCardRequest.SendInteractiveCardRequestCardData cardData, Map<String, PrivateDataValue> cardPrivateData,
                                      SendInteractiveCardRequest.SendInteractiveCardRequestCardOptions sendOptions,
                                      List<String> receiverUserIdList, Map<String, String> atOpenIds) {
        com.aliyun.dingtalkim_1_0.Client client;
        try {
            client = ClientSample.createClient();
        } catch (Exception e) {
            LOG.error("生成client报错");
            return null;
        }
        if (Objects.isNull(accessToken)) {
            LOG.error("accessToken为空");
            return null;
        }
        SendInteractiveCardHeaders sendInteractiveCardHeaders = new SendInteractiveCardHeaders();
        sendInteractiveCardHeaders.xAcsDingtalkAccessToken = accessToken;
        SendInteractiveCardRequest sendInteractiveCardRequest = new SendInteractiveCardRequest();
        sendInteractiveCardRequest.setCardTemplateId(cardTemplateId);
        sendInteractiveCardRequest.setOpenConversationId(openConversationId);
        sendInteractiveCardRequest.setReceiverUserIdList(receiverUserIdList);
        sendInteractiveCardRequest.setOutTrackId(cardBizId);
        sendInteractiveCardRequest.setRobotCode(robotCode);
        sendInteractiveCardRequest.setConversationType(BasicConstant.ONE);
        sendInteractiveCardRequest.setCallbackRouteKey(callbackRouteKey);
        sendInteractiveCardRequest.setCardData(cardData);
        sendInteractiveCardRequest.setPrivateData(cardPrivateData);
        sendInteractiveCardRequest.setCardOptions(sendOptions);
        sendInteractiveCardRequest.setAtOpenIds(atOpenIds);
        SendInteractiveCardResponse cardResponse;
        LOG.info("------------sendInteractiveCardRequest：{}",JSON.toJSONString(sendInteractiveCardRequest));
        try {
            cardResponse = client.sendInteractiveCardWithOptions(sendInteractiveCardRequest, sendInteractiveCardHeaders, new RuntimeOptions());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("发送互动卡片失败TeaException", err);
            cardResponse = null;
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("发送互动卡片失败Exception", err);
            cardResponse = null;
        }

        if (Objects.isNull(cardResponse) || !cardResponse.getBody().getSuccess()) {
            return null;
        }

        return cardResponse.getBody().getResult().getProcessQueryKey();
    }

    /***
     * 更新互动卡片
     *
     * @param: accessToken
     * @param: cardBizId
     * @param: cardData
     * @param: cardPrivateData
     * @throws
     * @return boolean
     * @author hongxiao
     * @date 2022-06-13 14:53
     * @since
     * @version
     */
    public boolean updateInteractiveCard(String accessToken, String cardBizId, UpdateInteractiveCardRequest.UpdateInteractiveCardRequestCardData cardData, Map<String, PrivateDataValue> cardPrivateData) {
        com.aliyun.dingtalkim_1_0.Client client;
        try {
            client = ClientSample.createClient();
        } catch (Exception e) {
            LOG.error("生成client报错");
            return false;
        }
        if (Objects.isNull(accessToken)) {
            LOG.error("accessToken为空");
            return false;
        }
        UpdateInteractiveCardHeaders updateInteractiveCardHeaders = new UpdateInteractiveCardHeaders();
        updateInteractiveCardHeaders.xAcsDingtalkAccessToken = accessToken;
        UpdateInteractiveCardRequest updateInteractiveCardRequest = new UpdateInteractiveCardRequest();

        updateInteractiveCardRequest.setOutTrackId(cardBizId);
        updateInteractiveCardRequest.setCardData(cardData);
        updateInteractiveCardRequest.setPrivateData(cardPrivateData);

        UpdateInteractiveCardResponse cardResponse;
        try {
            cardResponse = client.updateInteractiveCardWithOptions(updateInteractiveCardRequest, updateInteractiveCardHeaders, new RuntimeOptions());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("更新互动卡片失败TeaException", err);
            cardResponse = null;
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("更新互动卡片失败Exception", err);
            cardResponse = null;
        }

        return Objects.nonNull(cardResponse) ? Boolean.valueOf(cardResponse.getBody().getSuccess()) : false;
    }


    /***
     * 创建互动卡片实体
     *
     * @param: accessToken
     * @param: cardTemplateId
     * @param: openConversationId
     * @param: robotCode
     * @param: cardBizId
     * @param: cardData
     * @param: cardPrivateData
     * @throws
     * @return java.lang.String
     * @author hongxiao
     * @date 2022-06-13 11:12
     * @since
     * @version
     */
    public String createInteractiveCardInstance(String accessToken, String cardTemplateId, String openConversationId, String robotCode, String callbackRouteKey, String cardBizId,
                                                List<String> receiverUserIdList, InteractiveCardCreateInstanceRequest.InteractiveCardCreateInstanceRequestCardData cardData,
                                                Map<String, PrivateDataValue> cardPrivateData) {
        com.aliyun.dingtalkim_1_0.Client client;
        try {
            client = ClientSample.createClient();
        } catch (Exception e) {
            LOG.error("生成client报错");
            return null;
        }
        if (Objects.isNull(accessToken)) {
            LOG.error("accessToken为空");
            return null;
        }
        InteractiveCardCreateInstanceHeaders interactiveCardCreateInstanceHeaders = new InteractiveCardCreateInstanceHeaders();
        interactiveCardCreateInstanceHeaders.xAcsDingtalkAccessToken = accessToken;
        InteractiveCardCreateInstanceRequest interactiveCardCreateInstanceRequest = new InteractiveCardCreateInstanceRequest();

        interactiveCardCreateInstanceRequest.setCardTemplateId(cardTemplateId);
        interactiveCardCreateInstanceRequest.setOpenConversationId(openConversationId);

        interactiveCardCreateInstanceRequest.setOutTrackId(cardBizId);
        interactiveCardCreateInstanceRequest.setRobotCode(robotCode);
        interactiveCardCreateInstanceRequest.setConversationType(BasicConstant.ONE);
        interactiveCardCreateInstanceRequest.setReceiverUserIdList(receiverUserIdList);
        // 回调地址
        interactiveCardCreateInstanceRequest.setCallbackRouteKey(callbackRouteKey);
        interactiveCardCreateInstanceRequest.setCardData(cardData);
        interactiveCardCreateInstanceRequest.setPrivateData(cardPrivateData);

        LOG.info("--------interactiveCardCreateInstanceRequest:{}", JSON.toJSONString(interactiveCardCreateInstanceRequest));

        InteractiveCardCreateInstanceResponse cardResponse;
        try {
            cardResponse = client.interactiveCardCreateInstanceWithOptions(interactiveCardCreateInstanceRequest, interactiveCardCreateInstanceHeaders, new RuntimeOptions());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("创建互动卡片失败TeaException", err);
            cardResponse = null;
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("创建互动卡片失败Exception", err);
            cardResponse = null;
        }

        if (Objects.isNull(cardResponse)) {
            return null;
        }

        return cardResponse.getBody().getProcessQueryKey();
    }

    /***
     * 开启互动卡片置顶
     *
     * @param: accessToken
     * @param: openConversationId
     * @param: coolAppCode
     * @param: cardBizId
     * @param: receiverUserIdList
     * @throws
     * @return boolean
     * @author hongxiao
     * @date 2022-06-13 11:11
     * @since
     * @version
     */
    public boolean interactiveCardTopBoxesOpen(String accessToken, String openConversationId, String coolAppCode, String cardBizId, List<String> receiverUserIdList) {
        com.aliyun.dingtalkim_1_0.Client client;
        try {
            client = ClientSample.createClient();
        } catch (Exception e) {
            LOG.error("生成client报错");
            return false;
        }
        if (Objects.isNull(accessToken)) {
            LOG.error("accessToken为空");
            return false;
        }
        TopboxOpenHeaders topboxOpenHeaders = new TopboxOpenHeaders();
        topboxOpenHeaders.xAcsDingtalkAccessToken = accessToken;
        TopboxOpenRequest topboxOpenRequest = new TopboxOpenRequest();

        topboxOpenRequest.setOpenConversationId(openConversationId);
        topboxOpenRequest.setOutTrackId(cardBizId);
        topboxOpenRequest.setCoolAppCode(coolAppCode);
        topboxOpenRequest.setReceiverUserIdList(receiverUserIdList);
        topboxOpenRequest.setPlatforms("ios|mac|android|win");
        // 过滤时间
        topboxOpenRequest.setExpiredTime(null);

        TopboxOpenResponse topboxOpenResponse;
        try {
            topboxOpenResponse = client.topboxOpenWithOptions(topboxOpenRequest, topboxOpenHeaders, new RuntimeOptions());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("开启互动卡片置顶失败TeaException", err);
            topboxOpenResponse = null;
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("开启互动卡片置顶失败Exception", err);
            topboxOpenResponse = null;
        }

        return Objects.nonNull(topboxOpenResponse);
    }

    /***
     * 关闭互动卡片置顶
     *
     * @param: accessToken
     * @param: openConversationId
     * @param: coolAppCode
     * @param: cardBizId
     * @throws
     * @return boolean
     * @author hongxiao
     * @date 2022-06-13 11:11
     * @since
     * @version
     */
    public boolean interactiveCardTopBoxesClose(String accessToken, String openConversationId, String coolAppCode, String cardBizId, List<String> userIds) {
        com.aliyun.dingtalkim_1_0.Client client;
        try {
            client = ClientSample.createClient();
        } catch (Exception e) {
            LOG.error("生成client报错");
            return false;
        }
        if (Objects.isNull(accessToken)) {
            LOG.error("accessToken为空");
            return false;
        }
        TopboxCloseHeaders topboxCloseHeaders = new TopboxCloseHeaders();
        topboxCloseHeaders.xAcsDingtalkAccessToken = accessToken;
        TopboxCloseRequest topboxCloseRequest = new TopboxCloseRequest();

        topboxCloseRequest.setOpenConversationId(openConversationId);
        topboxCloseRequest.setOutTrackId(cardBizId);
        topboxCloseRequest.setCoolAppCode(coolAppCode);
//        topboxCloseRequest.setReceiverUserIdList(userIds);

        TopboxCloseResponse topboxCloseResponse;
        try {
            topboxCloseResponse = client.topboxCloseWithOptions(topboxCloseRequest, topboxCloseHeaders, new RuntimeOptions());
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("关闭互动卡片置顶失败TeaException", err);
            topboxCloseResponse = null;
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
            LOG.error("关闭互动卡片置顶失败Exception", err);
            topboxCloseResponse = null;
        }

        return Objects.nonNull(topboxCloseResponse);
    }
}
